package com.hx.miniblog.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author yangGuoQing 2021/12/28 15:02
 */

//http://localhost:8080/doc.html   swagger访问地址
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${config.jwt.name}")
    private String jwtHeader;

    @Value("${swagger.enable}")
    private Boolean enable;

    @Bean
    public Docket createRestApi() {
        ParameterBuilder jwtTokenPar = new ParameterBuilder();
        List<Parameter> pars = Lists.newArrayList();
        jwtTokenPar.name(jwtHeader).description("JWT token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(jwtTokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hx.miniblog.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("miniblog")
                .description("后端数据")
                .version("V1.0")
                .build();
    }
}